﻿@{
    ViewBag.Title = "订单详情";
}
@using Himall.Core;

@model Himall.Model.OrderInfo

<div class="container">
    <div class="list-group">
        <div class="list-group-item list-group-item-heading">订单状态：<span style="color:blue;">@Model.OrderStatus.ToDescription()</span><br/>
            订单编号：@Model.Id <br />成交时间：@Model.OrderDate.ToString("yyyy-MM-dd HH:mm:ss")</div>
        </div>
    <div class="list-group">
        <div class="list-group-item list-group-item-text">
            <p>商品金额：@((Model.OrderTotalAmount - Model.RefundTotalAmount).ToString("F2"))</p>
            <p><span>联系电话：</span>@Model.CellPhone</p>
            <p><span>收 货 人：</span>@Model.ShipTo</p>
            <p><span>收货地址：</span>@Model.RegionFullName @Model.Address</p>
        </div>
    </div>
    @{var hasExpressStatus = !string.IsNullOrWhiteSpace(Model.ShipOrderNumber);}

    <div class="list-group hide" expressinfo>
        <div class="list-group-item list-group-item-text">
            <p>
                <span>物流信息：</span><br />
                <span id="expressTopInfo" style="width:100%;text-align:left">正在加载...</span>
            </p>
        </div>
    </div>

    <ul class="order-list">
        @{var detail = ViewBag.Detail as Himall.Web.Areas.Mobile.OrderDetail;}
        <li>
            <h6>@detail.ShopName<i class="glyphicon pull-right gray"></i></h6>
            @foreach (var orderItem in detail.OrderItems)
            {
                <div class="order-goods clearfix">
                    <a href="@Url.Action("detail","product")/@orderItem.ProductId">
                        <img width="50" src="@orderItem.ProductImage" />
                        <p>@orderItem.ProductName</p>
                        <p><span class="red">¥@orderItem.Price.ToString("F2") </span></p>
                        <p><em>@(orderItem.Count)件</em></p>
                    </a>
                </div>
            }
            <p class="order-info acount">
            运费：¥@Model.Freight.ToString("F2") <br />
            实付： <span class="red">¥@((Model.OrderTotalAmount-Model.RefundTotalAmount).ToString("F2"))</span></p>
            
        </li>
    </ul>

    <div class="custom-dialog" id="paymentsChooser">
    </div>
    <div class="cover"></div>

    <footer class="footer-buy">
    @if (hasExpressStatus)
    {
        <a class="btn btn-default btn-sm" href="@Url.Action("ExpressInfo")?orderId=@Model.Id">查看物流</a>
    }
    @if (Model.OrderStatus == Himall.Model.OrderInfo.OrderOperateStatus.WaitPay)//待支付
    {
        <a class="btn btn-default btn-sm" href="javascript:cancelOrder('@Model.Id')">取消订单</a>
        <a class="btn btn-primary btn-sm" href="javascript:payOrder('@Model.Id')">付款</a>
    }
    else if (Model.OrderStatus == Himall.Model.OrderInfo.OrderOperateStatus.WaitReceiving)//待收货
    {
        <a class="btn btn-primary btn-sm" href="javascript:confirm('@Model.Id')">确认收货</a>
    }
</footer>
</div>
<input type="hidden" value="@Model.ExpressCompanyName" id="expressCompanyName" />
<input type="hidden" value="@Model.ShipOrderNumber" id="shipOrderNumber" />
<input type="hidden" value="@hasExpressStatus" id="hasExpressStatus" />
<script>

    //显示物流信息
    var expressValidStatus = $('#hasExpressStatus').val();//获取当前状态
    if (expressValidStatus) {//包含快递信息的状态，即已发货后的状态
        $('[expressInfo]').removeClass('hide');
        var expressCompanyName = $('#expressCompanyName').val();
        var shipOrderNumber = $('#shipOrderNumber').val();

        $.post('/common/ExpressData/searchTop',
            { expressCompanyName: expressCompanyName, shipOrderNumber: shipOrderNumber },
            function (expressData) {
                if (expressData.success) {
                    var html = '<ul class="goods_interflow">';
                    $.each(expressData.data, function (i, data) {
                            $('#expressTopInfo').html('<label>' + data.content + ' ' + data.time + '</label>');
                            style = ' class="active"';
                    });
                }
                else {
                    $('#expressTopInfo').html('<label class="red">' + expressData.msg + '</label>');
                }
            }
        );
    }

    function cancelOrder(orderId) {
        $.dialog.confirm("确定取消该订单吗？", function () {
            var loading = showLoading();
            $.ajax({
                type: 'post',
                url: '/' + areaName + '/Order/CloseOrder',
                dataType: 'json',
                data: { orderId: orderId },
                success: function (d) {
                    loading.close();
                    if (d.success) {
                        $.dialog.succeedTips("取消成功！", function () {
                            window.location.href = window.location.href;
                        }, 1);
                    }
                    else {
                        $.dialog.errorTips("取消失败！", '', 2);
                    }
                },
                error: function () {
                    loading.close();
                }
            });
        });
    }


    function confirm(orderId) {
        $.dialog.confirm("你确定收到货了吗？", function () {
            var loading = showLoading();
            $.ajax({
                type: 'post',
                url: '/' + areaName + '/Order/ConfirmOrder',
                dataType: 'json',
                data: { orderId: orderId },
                success: function (d) {
                    loading.close();
                    if (d.success) {
                        $.dialog.succeedTips("确认成功！", function () {
                            window.location.href = window.location.href;
                        }, 1);
                    }
                    else {
                        $.dialog.errorTips("确认失败！", '', 2);
                    }
                },
                error: function () {
                    loading.close();
                }
            });
        });

     
    }

    function payOrder(orderId)
    {
        var loading = showLoading();
        if(@Model.OrderTotalAmount==0)
        {
            loading && loading.close();
            $.dialog.confirm('您确定用积分抵扣全部金额吗？', function () {
                ajaxRequest({
                    type: 'POST',
                    url: '/' + areaName + '/Order/PayOrderByIntegral',
                    param: { orderIds: orderId },
                    dataType: 'json',
                    success: function (data) {
                        if (data.success == true) {
                            $.dialog.succeedTips('支付成功！', function () {
                                location.href = '/' + areaName + '/Member/Orders';
                            },0.5);
                                    
                        }
                    },
                    error: function (data) { $.dialog.tips('支付失败,请稍候尝试.', null, 0.5); }
                });
            });
        }
        else
        {
            $.post('/' + areaName + '/payment/get', { orderIds: orderId },
                           function (payments) {
                               loading && loading.close();
                               if (payments.length > 0) {
                                   var html = '';
                                   $.each(payments, function (i, payment) {
                                       html += '<a class="btn btn-success btn-block" href="' + payment.url + '">' + payment.name + '</a>';
                                   });
                                   $('#paymentsChooser').html(html);
                                   $('.cover').fadeIn();
                                   $('.custom-dialog').show();
                               }
                               else {
                                   $.dialog.tips('没有可用的支付方式，请稍候再试');
                               }
                           });
        }
    }
    $('.cover').click(function () {
        $('.cover').fadeOut();
        $('.custom-dialog').hide();
    });
</script>